/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model.entity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;

import model.entity.relatorio.RespostaPontuacao;

import org.hibernate.annotations.LazyCollection;
import org.hibernate.annotations.LazyCollectionOption;

/**
 *
 * @author Alex
 */
@Entity
@Table(catalog = "nasa", schema = "")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "Resultado.findAll", query = "SELECT r FROM Resultado r"),
    @NamedQuery(name = "Resultado.findByResultId", query = "SELECT r FROM Resultado r WHERE r.resultId = :resultId"),
    @NamedQuery(name = "Resultado.findByResultData", query = "SELECT r FROM Resultado r WHERE r.resultData = :resultData")})
public class Resultado implements Serializable, Comparable<Resultado> {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "result_id", nullable = false)
    private Integer resultId;
    @Basic(optional = false)
    @Column(name = "result_data", nullable = false)
    @Temporal(TemporalType.TIMESTAMP)
    private Date resultData;
    @OneToMany(mappedBy = "respResultado")
    @LazyCollection(LazyCollectionOption.FALSE)
    private List<Resposta> respostaList;
    @JoinColumn(name = "result_questionario", referencedColumnName = "quest_id", nullable = false)
    @ManyToOne(optional = false)
    private Questionario resultQuestionario;
    @JoinColumn(name = "result_participante", referencedColumnName = "part_email", nullable = false)
    @ManyToOne(optional = false)
    private Participante resultParticipante;
    @JoinColumn(name = "result_grupo", referencedColumnName = "grupo_id")
    @ManyToOne
    private Grupo resultGrupo;

    public Resultado() {
    }

    public Resultado(Integer resultId) {
        this.resultId = resultId;
    }

    public Resultado(Integer resultId, Date resultData) {
        this.resultId = resultId;
        this.resultData = resultData;
    }

    public Integer getResultId() {
        return resultId;
    }

    public void setResultId(Integer resultId) {
        this.resultId = resultId;
    }

    public Date getResultData() {
        return resultData;
    }

    public void setResultData(Date resultData) {
        this.resultData = resultData;
    }

    @XmlTransient
    public List<Resposta> getRespostaList() {
        return respostaList;
    }

    public void setRespostaList(List<Resposta> respostaList) {
        this.respostaList = respostaList;
    }

    public Questionario getResultQuestionario() {
        return resultQuestionario;
    }

    public void setResultQuestionario(Questionario resultQuestionario) {
        this.resultQuestionario = resultQuestionario;
    }

    public Participante getResultParticipante() {
        return resultParticipante;
    }

    public void setResultParticipante(Participante resultParticipante) {
        this.resultParticipante = resultParticipante;
    }

    public Grupo getResultGrupo() {
        return resultGrupo;
    }

    public void setResultGrupo(Grupo resultGrupo) {
        this.resultGrupo = resultGrupo;
    }
    
    public Integer getTotal(){
    	Integer total = 0;
    	for (Resposta resposta : respostaList)
			total += Integer.parseInt(resposta.getRespResposta());
    	return total;
    }
    
    public List<RespostaPontuacao> getMaioresPontuacoes(){
    	List<RespostaPontuacao> respostasPontuacao = new ArrayList<RespostaPontuacao>();
    	List<Resposta> respostasOrdenadas = new ArrayList<Resposta>(respostaList);
    	Collections.sort(respostasOrdenadas);
    	for (int i = 0; i < 3; i++) {
    		RespostaPontuacao respostaPontuacao = new RespostaPontuacao(respostaList.indexOf(respostasOrdenadas.get(i)), Integer.parseInt(respostasOrdenadas.get(i).getRespResposta()));
    		respostasPontuacao.add(respostaPontuacao);
		}
    	return respostasPontuacao;
    }
    
	public int getPontuacaoDemandaMental(){
		return Integer.parseInt(getRespostaList().get(0).getRespResposta());
	}
	
	public int getPontuacaoDemandaFisica(){
		return Integer.parseInt(getRespostaList().get(1).getRespResposta());
	}
	
	public int getPontuacaoDemandaTemporal(){
		return Integer.parseInt(getRespostaList().get(2).getRespResposta());
	}
	
	public int getPontuacaoPerformance(){
		return Integer.parseInt(getRespostaList().get(3).getRespResposta());
	}
	
	public int getPontuacaoEsforco(){
		return Integer.parseInt(getRespostaList().get(4).getRespResposta());
	}
	
	public int getPontuacaoFrustracao(){
		return Integer.parseInt(getRespostaList().get(5).getRespResposta());
	}

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (resultId != null ? resultId.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Resultado)) {
            return false;
        }
        Resultado other = (Resultado) object;
        if ((this.resultId == null && other.resultId != null) || (this.resultId != null && !this.resultId.equals(other.resultId))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "entidades.Resultado[ resultId=" + resultId + " ]";
    }

	@Override
	public int compareTo(Resultado o) {
//		if (o.getTotal() > this.getTotal())
//			return 1;
//		else if (o.getTotal() < this.getTotal())
//			return -1;
//		return 0;
		return  o.getTotal() - this.getTotal();
	}
}
